package org.quasar.airNova.presentationLayer;

import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import org.quasar.airNova.persistenceLayer.Database;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;

public class CancelarReserva extends JPanel {
	private Gui gui;
	private JComboBox nReserva;
	private JComboBox nCliente;

	/**
	 * Create the panel.
	 */
	public CancelarReserva(Gui gui) {
		this.setGui(gui);
		setLayout(new BorderLayout(0, 0));
		this.setSize(500, 350);
		JPanel panel = new JPanel();
		add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JLabel lblNewLabel = new JLabel("N\u00FAmero de Cliente");
		lblNewLabel.setBounds(40, 76, 151, 14);
		panel.add(lblNewLabel);

		JLabel lblNewLabel_2 = new JLabel("N\u00FAmero da Reserva");
		lblNewLabel_2.setBounds(40, 101, 126, 14);
		panel.add(lblNewLabel_2);

		JButton btnNewButton = new JButton("Submeter");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				if (nCliente.getSelectedItem() != null
						&& nReserva.getSelectedItem() != null) {
					AirNova_Database.cancelarReserva(
							(String) nCliente.getSelectedItem(),
							(String) nReserva.getSelectedItem());
					generateComboBoxReserva((String) nCliente.getSelectedItem());
				}
			}
		});
		btnNewButton.setBounds(44, 217, 98, 23);
		panel.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("Cancelar");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getGui().getTabbedPane().removeTabAt(
						getGui().getTabbedPane().getSelectedIndex());
			}
		});
		btnNewButton_1.setBounds(152, 217, 89, 23);
		panel.add(btnNewButton_1);

		JLabel lblNewLabel_3 = new JLabel("Cancelar Reserva");
		lblNewLabel_3.setFont(new Font("Tahoma", Font.PLAIN, 17));
		lblNewLabel_3.setBounds(30, 24, 129, 32);
		panel.add(lblNewLabel_3);

		if (Tools.getClienteCodigos() != null) {
			nCliente = new JComboBox(Tools.getClienteCodigos());
		} else
			nCliente = new JComboBox();
		nCliente.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				generateComboBoxReserva((String) nCliente.getSelectedItem());
			}

		});
		nCliente.setBounds(231, 73, 203, 20);
		panel.add(nCliente);

		nReserva = new JComboBox();
		nReserva.setBounds(231, 98, 203, 20);
		panel.add(nReserva);
	}

	private void generateComboBoxReserva(String nCliente) {
		String[] reservasN_ReservaByCliente = Tools
				.getReservasN_ReservaByCliente(nCliente);
		nReserva.removeAllItems();
		if (reservasN_ReservaByCliente != null) {
			for (int i = 0; i < reservasN_ReservaByCliente.length; i++) {
				nReserva.addItem(reservasN_ReservaByCliente[i]);
			}
		}
	}

	public Gui getGui() {
		return gui;
	}

	public void setGui(Gui gui) {
		this.gui = gui;
	}
}
